using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using DTO;
using System.Configuration;
using System.Data;
namespace DAL
{
    public class LOAINGUOIDUNG_DAL
    {
        
        private SqlConnection cn = null;
        public LOAINGUOIDUNG_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"]
                 .ConnectionString);
            cn.Open();
          
        }
        public void insert(LoaiNguoiDung_DTO loainguoid)
        {
            SqlCommand cmd = new SqlCommand("LOAINGUOIDUNG_INSERT", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@TENLOAI_NGUOIDUNG", loainguoid.TenLoai_NguoiDung);
            cmd.ExecuteNonQuery();
        }
        public void update(LoaiNguoiDung_DTO loainguoid)
        {
            SqlCommand cmd = new SqlCommand("LOAINGUOIDUNG_UPDATE", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MALOAI_NGUOIDUNG", loainguoid.MaLoai_NguoiDung);
            cmd.Parameters.AddWithValue("@TENLOAI_NGUOIDUNG", loainguoid.TenLoai_NguoiDung);
            cmd.ExecuteNonQuery();
        }
        public void delete(int maloainguoidung)
        {
            SqlCommand cmd = new SqlCommand("LOAINGUOIDUNG_DELETE", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MALOAI_NGUOIDUNG", maloainguoidung);
            cmd.ExecuteNonQuery();
        }
        public tbLoaiNguoiDungCollection Get(String where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("LOAINGUOIDUNG_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@WHERE", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PAGESIZE", pageSize);
            cm.Parameters.AddWithValue("@ORDERBY", orderBy);
            cm.Parameters.AddWithValue("@ORDERDIRECTION", orderDirection);

            //cm.Parameters .Add (new SqlParameter ("@TOTALRECORD",DbType.Int32)).Direction=ParameterDirection.Output 
            cm.Parameters.Add(new SqlParameter("@TOTALRECORD", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbLoaiNguoiDungCollection lndungCollection = new tbLoaiNguoiDungCollection();
            while (dr.Read())
            {
                LoaiNguoiDung_DTO LNDUNG = new LoaiNguoiDung_DTO();
                LNDUNG.MaLoai_NguoiDung = int.Parse(dr.GetValue(0).ToString());
                LNDUNG.TenLoai_NguoiDung = dr.GetValue(1).ToString();
                lndungCollection.Add(LNDUNG);


            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TOTALRECORD"].Value.ToString());
            return lndungCollection;


        }
    }
}
